package data
{
	import mx.utils.StringUtil;
	
	import spark.globalization.StringTools;

	public final class RateData
	{
		
		[Embed(source="data.xml",mimeType="application/octet-stream")]
		private var DATACLASS:Class;
		private var dataXML:XML
		private var ratedata:Vector.<RateLevel>;
		private static var _rateData:RateData
		public function RateData()
		{
			init();
		}
		
		private function init():void{
			ratedata = new Vector.<RateLevel>;
			var rateLevel:RateLevel;
			dataXML =XML(new DATACLASS());
			for each(var level:XML in dataXML.children()){
				rateLevel = new RateLevel();
				rateLevel.level = int(level.@level);
				rateLevel.baseMoney = int(level.@baseMoney);
					for each(var sex:XML in level.children()){
						if(Config.MAN == int(sex.@value)){
							rateLevel.manRate = getRateYearSex(sex.children());
						}else{
							rateLevel.womenRate =getRateYearSex(sex.children());
						}
					}
				
				ratedata.push(rateLevel);
			}
		}
		
		private function getRateYearSex(xmlist:XMLList):Vector.<RateYear>{
			var rateYearSex:Vector.<RateYear> = new Vector.<RateYear>();
			var yearSex:RateYear;
			if(xmlist!=null){
				for each(var year:XML in xmlist){
					yearSex = new RateYear();
					yearSex.year = int(year.@value);
					yearSex.ageRate = getAgeRate(year.toString());
					rateYearSex.push(yearSex);
				}
			}
			return rateYearSex;
		}
		
		private function getAgeRate(str:String):Vector.<Rate>{
			var rates:Vector.<Rate> = new Vector.<Rate>();
			var rate:Rate;
			if(str!=null && str!=""){
				var datas:Array = str.split("\n");
				if(datas!=null){
					var age:int = datas.length;
					while(age>=0){
						rate = new Rate();
						rate.age = int(age);
						rate.rate = int(datas[age]);
						rates.unshift(rate);
						age--;
					}
				}
			}
			return rates;
		}
		
		public function getRateBylevel(leve:int,age:int, sex:int):Rate{
			for each(var rateLevel:RateLevel in ratedata){
				if(rateLevel.level == leve){
					return rateLevel.getRateByeAge(sex,age);
				}
			}
			return null;
		}
		
		public function getRateByqujian(leve:int,age:int, sex:int,qj:int):Rate{
			for each(var rateLevel:RateLevel in ratedata){
				if(rateLevel.level == leve){
					return rateLevel.getRateByeQJ(sex,age,qj);
				}
			}
			return null;
		}
		
		public static function getRateData():RateData{
			if(_rateData==null){
				_rateData = new RateData();
			}
			return _rateData;
		}
		
	}
}